package com.cloud.threads;

import java.io.IOException;
import java.util.Date;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.cloud.entity.ResultVo;
import com.cz073586.tools.http.HttpManagerUtil;
import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.Tools;
import com.local.common.LogInfoConstan;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.LocalStation;
import com.local.model.Log;
import com.local.service.face.LogService;
import com.local.util.HttpClientUtil;

/**
 * 类描述: 岗亭数据同步到云平台
 * 
 * @ClassName: UpdateStationRunble
 * @author Administrator
 * @date 2015年10月10日 下午7:45:03
 */
public class UpdateStationRunble implements Runnable
{
	
	private Log4jUtil log = Log4jUtil.getLog4j(UpdateStationRunble.class);
	private LocalStation localStation;
	private LogService logServier;
	
	public UpdateStationRunble(LocalStation localStation,LogService logServier)
	{
		super();
		this.localStation = localStation;
		this.logServier=logServier;
	}
	
	@Override
	public void run()
	{
		// 发送请求
					CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
							.getHttpClient(null);
		try
		{
			
			if (Tools.isEmpty(this.localStation))
			{
				return;
			}
			// 获取请求路径
			String uri = CloudConfigManager.instance().getHost()+CloudConfigManager.instance().getCfgValueByKey(
					"sync_station_update");
			
			
			HttpPost post = new HttpPost(uri);
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			//获取系统当前停车场编号。
			localStation.setExchangeUuid(CloudConfigManager.instance().getExchangeId());
			
			String sendData = JsonUtil.getJsonString(localStation);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity(), "UTF-8");
			
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			
			if (resultVo.getResult().equals("success"))
			{
				System.out.println("修改岗亭纪录成功..");
			} else
			{
				System.out.println("修改岗亭纪录失败.");
				addErrorLog(localStation.getStationUuid(),"修改岗亭纪录失败.");
			}
			
		} catch (Throwable t)
		{
			this.log.error("", t);
      addErrorLog(this.localStation.getStationUuid(), "修改岗亭纪录失败.");
      try
      {
         if (httpClient != null)
         {
          httpClient.close();
       }
      }
      catch (IOException localIOException1)
      {
       }
		}finally{
			try
			{
				if(null!=httpClient)
				{
				httpClient.close();
				}
			} catch (IOException localIOException2)
			{
				
			}
		}
		
	}
	public void addErrorLog(String businessUuid,String deScript)
	{
		Log logInfo = new Log();
		logInfo.setCreaterAt(new Date());
		logInfo.setSummary("修改岗亭异常");
		logInfo.setDescript(deScript);
		logInfo.setLevel("警告");
		logInfo.setSrc("系统");
		logInfo.setStatus(Integer.valueOf(1));
		logInfo.setCls("com.cloud.threads.UpdateStationRunble");
		logInfo.setModule(Integer.valueOf(13));
		logInfo.setBusinessUuid(businessUuid);
		this.logServier.addLogInfo(logInfo);

	}
}
